package com.ruoyi.canteen.controller.v1;

import com.ruoyi.canteen.constant.CpStatus;
import com.ruoyi.canteen.domain.CanteenCp;
import com.ruoyi.canteen.service.ICanteenCpService;
import com.ruoyi.canteen.vo.CanteenCpVo;
import com.ruoyi.canteen.vo.CanteenOrderItemVo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Ryan
 * @Date 2021/9/9 9:48
 * @Version 1.0
 */
@RestController
@RequestMapping("/v1/cp")
public class CpApiController extends BaseController {

    @Autowired
    private ICanteenCpService canteenCpService;

    /**
     * 通过菜品类别 查询菜品列表
     */
    @GetMapping("/listByCpClass/{cpClass}")
    public TableDataInfo listByCpClass(@PathVariable("cpClass") Integer cpClass)
    {
        List<CanteenCp> list = canteenCpService.selectCpListByClass(cpClass);
        return getDataTable(list);
    }

    /**
     * 通过菜品类别 查询菜品VO列表
     */
    @GetMapping("/listVoByCpClass/{cpClass}")
    public TableDataInfo listVoByCpClass(@PathVariable("cpClass") Integer cpClass)
    {
        List<CanteenCpVo> list = canteenCpService.selectCpVoListByClass(cpClass);
        return getDataTable(list);
    }

    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        CanteenCp cp =  canteenCpService.selectCanteenCpById(id);
        return AjaxResult.success(cp);
    }

    @PostMapping(value = "/verifyCpStatus")
    public AjaxResult verifyCpStatus(@RequestBody List<CanteenCpVo> cpVos){
        // 菜品状态默认:都为有效
        Boolean cpStatus = true;
        List<CanteenCpVo> invalidCpList = new ArrayList<CanteenCpVo>();
        for(CanteenCpVo cp : cpVos){
            // 查询下当前菜品状态是否为上架
            CanteenCp dbCp =  canteenCpService.selectCanteenCpById(cp.getId());
            if(dbCp.getStatus().intValue() != CpStatus.CP_STATUS_SJ){
                // 非上架状态，则加入无效菜单列表
                invalidCpList.add(cp);
            }
        }
        if(invalidCpList.size() > 0){
            cpStatus = false;
        }
        Map<String, Object> reMap = new HashMap<String, Object>();
        reMap.put("cpStatus", cpStatus);
        reMap.put("invalidCpList", invalidCpList);
        return AjaxResult.success(reMap);
    }


}
